var tgd = tgd || {};
tgd.Tools = tgd.Tools || {};
tgd.Tools.ContactDiff = tgd.Tools.ContactDiff || {};

/*
 * represents one Google account
 */
tgd.Tools.ContactDiff.Google = function() {

	var _scope = "http://www.google.com/m8/feeds";

	this.__defineGetter__("isAuthenticated", function() { return google.accounts.user.checkLogin(_scope) !== ""; });

	this.login = function() {
		if (google.accounts.user.checkLogin(_scope) === "") {
			var token = google.accounts.user.login(_scope);
		}
	}

	this.logout = function() {
		google.accounts.user.logout();
	}

	/*
	 * see http://code.google.com/apis/gdata/jsdoc/2.2/google/gdata/contacts/ContactsService.html
	 */
	this.getContacts = function(continuation, opt_errorHandler) {
        console.log('getContacts');
		var contactsFeedUri = 'http://www.google.com/m8/feeds/contacts/default/full';
		var query = new google.gdata.contacts.ContactQuery(contactsFeedUri);
		query.setMaxResults(300);
		tgd.Tools.ContactDiff.Google.ContactsService.getContactFeed(query, continuation, opt_errorHandler);
	}

	this.getGroups = function(continuation, opt_errorHandler) {
	    console.log('getGroups');
		var groupsFeedUri = 'http://www.google.com/m8/feeds/groups/default/full';
		var query = new google.gdata.contacts.ContactQuery(groupsFeedUri);
		query.setMaxResults(300);
		tgd.Tools.ContactDiff.Google.ContactsService.getContactGroupFeed(query, continuation, opt_errorHandler);
	}
}

google.load("gdata", "2.x", {packages: ['contacts']});
google.setOnLoadCallback(
	function() {
		console.log('onloadcallback');
		tgd.Tools.ContactDiff.Google.ContactsService = new google.gdata.contacts.ContactsService('tgd-Contact-Diff-1.0');
	}
);
